
"""Run Soya GUI

The script to run the PGame Soya GUI.
"""

#Soya imports
import soya, soya.sdlconst

#Python imports
import sys, os, os.path

#Our imports
from units import *
from pgameworld import pgameWorld as pgWorld
import pgEngine as eng
from boatbody import BoatBody
from utils import findDataDir

class GUIManager(soya.World):
    def __init__(self):
        super(GUIManager, self).__init__()
        #Graphics setup
        self.__setDataDir()
        #self.scene = self.__createScene()
        self.camera = self.__createCamera(self)
        self.__setLights(self)
        self.level = self.__createLevel(self)
        soya.set_root_widget(self.camera)
        
        self.game = None
    
    def __setDataDir(self):
        """Finds our data and tells soya where the data is."""
        #Locate where all our resources are, starting in the current directory.
        print "Finding data..."
        soya.path.append( findDataDir() )
        
    
    def __createScene(self):
        print "Creating scene..."
        #scene = soya.World()
        #real_world = pgWorld(scene)
        scene = pgWorld()
        return scene
    
    def __createCamera(self, scene):
        print "Creating camera..."
        camera = soya.Camera(scene)
        camera.set_xyz(0.0, 10.0, 5.0)
        camera.look_at( soya.Point(scene, 0, 0, 0) )
        return camera
    
    def __setLights(self, scene):
        print "Setting lights..."
        self.light = soya.Light(scene)
        self.light.set_xyz(2.0, 5.0, 0.0)
    
    def __createLevel(self, scene):
        print "Creating ocean.."
        wOcean = soya.World(scene)
        fiveFoot = feet(5)
        print fiveFoot
        f = soya.Face(wOcean, [soya.Vertex(wOcean,  fiveFoot, 0,  fiveFoot, diffuse = (0.0, 0.0, 1.0, .5)),
                              soya.Vertex(wOcean, -fiveFoot, 0,  fiveFoot, diffuse = (0.0, 0.0, 1.0, .5)),
                              soya.Vertex(wOcean, -fiveFoot, 0, -fiveFoot, diffuse = (0.0, 0.0, 1.0, .5)),
                              soya.Vertex(wOcean,  fiveFoot, 0, -fiveFoot, diffuse = (0.0, 0.0, 1.0, .5))
                              ])
        f.double_sided = True
        bOcean = soya.Body(scene, wOcean.to_model())
        return bOcean
    
    def startNewGame(self):
        """Starts a game in "single-player mode". NOTE: This is actually 
        pointless for anyone except developers."""
        print "Loading engine..."
        self.game = eng.game.PirateGame()
        print 'Adding player...'
        self.addPlayer(self)
    
    def addPlayer(self, scene):
        eoPlayer = self.game.addPlayer()
        eoBoat = eoPlayer.boats[-1]
        #TODO: Pick a model based on class, eg. load either model 2c1 or 2c5 for
        #      a class 2c boat
        print "Loading boat model..."
        wBoat = soya.Model.get("output")
        print 'Creating boat body...'
        self.bBoat = BoatBody(scene, wBoat, eoBoat)
        
    def begin_round(self):
        super(GUIManager, self).begin_round()
        
        #process events
        for event in soya.process_event():
            self.check_quit(event)
    
    def check_quit(self, event):
        """If the given event is a quit event, exits."""
        if event[0] == soya.sdlconst.KEYDOWN:
            if event[1] == soya.sdlconst.K_q:
                soya.MAIN_LOOP.stop()
            elif event[1] == soya.sdlconst.K_ESCAPE:
                soya.MAIN_LOOP.stop()
    
    def advance_time(self, proportion):
        soya.World.advance_time(self, proportion)
    
soya.init()
print "Loading GUI..."
g = GUIManager()
print "Starting game..."
g.startNewGame()
print "Starting main loop..."
soya.MainLoop(g).main_loop()